//
//  BaseViewController.m
//  XHNShangChengMoBan
//
//  Created by 张铁军 on 16/8/13.
//  Copyright © 2016年 ping. All rights reserved.
//

#import "BaseViewController.h"
#import "BaseLabel.h"
#import "DengLuViewController.h"


@interface BaseViewController ()

@end
#define ZFBkServerUrl   [NSString stringWithFormat:@"http://%@/cop/pay/payhook1.action",IP]
#define ZFBfangKeUrl    [NSString stringWithFormat:@"http://%@/pay/car/visiterfukuan.apple",IP]

@implementation BaseViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.automaticallyAdjustsScrollViewInsets = NO;
    // Do any additional setup after loading the view.
    [self ksc];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 15, 18)];
    [btn addTarget:self action:@selector(goBackTo) forControlEvents:UIControlEventTouchUpInside];
    [btn setImage:[UIImage imageNamed:@"you" ] forState:UIControlStateNormal];
    
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithCustomView:btn];
    
    self.navigationItem.leftBarButtonItem = backItem;
    [self.navigationController setNavigationBarHidden:YES animated:NO];
   
}
-(void)ksc{
    if (KScreenHeight == 480 ) {
        _kscH = 1;
        _kscW = 1;
    }
    else
    {
        _kscH = KScreenHeight / 568;
        _kscW = KScreenWidth / 320;
    }
}

-(void)drawNavWithTitle:(NSString *)Title{
    UILabel *labNav = [[UILabel alloc]initWithFrame:CGRectMake(0, 0,KScreenWidth , 64)];
    labNav.backgroundColor = ZONGCOLOR;
    [self.view addSubview:labNav];
    BaseLabel* lab = [[BaseLabel alloc]initWithFrame:CGRectMake(0, 0,180 ,  27)];
    lab.center = CGPointMake(KScreenWidth  / 2 , 43) ;
    lab.textAlignment = NSTextAlignmentCenter;
    //    lab.textColor = [UIColor whiteColor];
    lab.myfont = TITLEFONT;
    lab.text = Title;
    lab.backgroundColor = [UIColor clearColor];
    [self.view addSubview:lab];
}

-(void)drawNavWithTitle:(NSString *)Title WithColor:(UIColor*)color{
    UILabel *labNav = [[UILabel alloc]initWithFrame:CGRectMake(0, 0,KScreenWidth , 64)];
    labNav.backgroundColor = color;
    [self.view addSubview:labNav];
    BaseLabel* lab = [[BaseLabel alloc]initWithFrame:CGRectMake(0, 0,180 ,  27)];
    lab.center = CGPointMake(KScreenWidth  / 2 , 43) ;
    lab.textAlignment = NSTextAlignmentCenter;
    //    lab.textColor = ZONGCOLOR;
    lab.myfont = TITLEFONT;
    lab.text = Title;
    lab.backgroundColor = [UIColor clearColor];
    [self.view addSubview:lab];
    
    
//    [self.view addSubview:[UILabel drawXiaHuaXianLabel:63.5]];
    
}
-(void)drawBackButton{
    UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(10, 20, 60, 44)];
    
    [btn addTarget:self action:@selector(clickBack) forControlEvents:UIControlEventTouchDown];
    BaseLabel *backLabel = [[BaseLabel alloc]initWithFrame:CGRectMake( 20, 5, 40, 34)];
    backLabel.myfont = TITLEFONT;
    
    //35  52
    UIImageView *btnImage = [[UIImageView alloc]initWithFrame:CGRectMake(10, 15, 7 ,15)];
    [btnImage setImage:[UIImage imageNamed:@"fanhui"]];
    [btn addSubview:btnImage];
//    [btn addSubview:backLabel];
    [self.view addSubview:btn];
}
-(void)clickBack{
    [self.navigationController popViewControllerAnimated:YES];
}


//支付宝支付
-(void)zhiFuZhiFuBaoWith:(NSString *)tradeNO and:(NSString *)productName and:(NSString*)productDescription and:(float)amount andType:(NSString *)type andCount:(NSString *)count andHuiDiaoZT:(int)huiDiaoZT anBlack:(void(^)(NSDictionary * resultDic))block{
    NSString *partner = @"2088421810907705";
    NSString *seller = @"3472807448@qq.com";
    NSString *privateKey = @"MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAK+lllEOjfC3JPEyaR17/YHdrv8XDB/0fXes8t7PDVleDcPfoOFFvhd70XMVd2kEWMY1HnPQgZ0l9a/B9gfk6TH1YFRnPdH+rnwToQa5offfBIEWepZZStJngXpGRIxjHuH3nC4+jfGLR/qccKKZDw+1/14w1wAsU5KH3jSAbU9LAgMBAAECgYAjioEBA83q0FQ0TfwBEE66kW3QhgxZBvLAXYmDViggViy43q5CBwew3sgB3m0cHdoMuUDhNGaVstGk771l2Ybp1Cg5D3wP+8FEPvDSgOJUFl6OfaTp2RlFoWQZZEDQNSVloGAoUiuJqrhje5syOm4ASBv1gSpU5eYBujuE2fd2+QJBANXl9RxCdhL72+Piy8lGXEzu3/IVumocIiOdRzxOGYUYHnuBbC6rVv5u2ch2HbxKAqkBXVGTVcJjjuDcl2mdbtUCQQDSODEBfISZaoq3M/sB3td/lAA7vrGePD7lITTdtlkdldDn7PQrua5LP+cWc3Ju/6V6j6o/2Vpy/kmljb66VhWfAkEArHfF4mpoYDg5/g7BuGIGTHYTllQUwjg7ArJvXN2wUBO5iGY7v4miFHd3HN64bTjT2kfiYcNF0/6IlyDm6V7qnQJAUvz726pcbnQw7sIGhWaDjEnRDcTqOZ1u9uV1CVHDPHq4TdatTdWviOVhROuucUNbAFdWzn3vxxW73S9n7Aev4QJASglFrGz2KQHzqMlQgep0MjL1OFWlZAX5sZF1+eQzbRxL0XzbGn2m1pSDpXFhWt+CjHTpEb0IJo6hqpQwTLNZiw==";
    
    
    
    //    NSString *partner = @"2088121257323332";
    //    NSString *seller = @"107911230@qq.com";
    //    NSString *privateKey = @"MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAKB1DrYt0obMxO7OM0pyhlPuKGhtLy5BN8SwvMzxLOQClujBqNTvlRurBP3e2IJvdhxu1rhg1H22SLVK6aw1BSaQhafXQzw/4VRbYLV7Pf3naak45DqoxgKcgOamKrUDidM2cQmTWtli3890MiP5iIeX8fC+XDjkBFQifzsfCIRPAgMBAAECgYBqDrVYqVT8EKEij1zLmoM7OrGuxGxpEGaCNae1drwLQcLpugeUJ3/RN+smuoM1Q+M3y/NVD3ukVDYezpNBZhIuahNA7taN0dPus4RWqtQ80TtWJCBkdZ8IMnO1dOxY5klETHHBWSMA2njpcjjFPuyJJlhZFvRmESHi+dM3bnTdcQJBAO1mXh0TYDkqDEDdoD6MoDV+ztyg/zfIfiqmBO60avhGSzMr2/rl10Yt3Ur5BFqRUjX4eCp8WG3uuLmaKI0XxfUCQQCtB2sX6Ea7JKIW1Hu0sQmB4btibeW5zUVd5G61YgsZ3y4CK7JD0KhAQvXvxFZoDaE7JnKi8o64TEKWYBhQ+nKzAkBWmpWSvdLIh9JYh4FB0ENRwKTuT71LOFTucgoympa7Y8ljLYjeiqJx86FbDGTONV1sWuxbc2k/EWtKKjbI4fdRAkEAmdECbxpZ5nvGbC0wW40WOpQFFJdFpUrJ45HyqOQjojg4mk1mB8syBw5/u0n61j1nm0GunxSps0R33V5Hv5Uk6QJAXrM4WcmOHRmbKSEvwwWbfuEgBEFSjcQrg6yL8QKzS+ozz+ni/LHb70izokiQp+dST+oPx970KCgZGi+4QmSItQ==";
    /*============================================================================*/
    /*============================================================================*/
    /*============================================================================*/
    
    //partner和seller获取失败,提示
    if ([partner length] == 0 ||
        [seller length] == 0 ||
        [privateKey length] == 0)
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示"
                                                        message:@"缺少partner或者seller或者私钥。"
                                                       delegate:self
                                              cancelButtonTitle:@"确定"
                                              otherButtonTitles:nil];
        [alert show];
        return;
    }
    
    
    if (!productName || [productName isEqualToString:@""]) {
        productName = @"商品";
    }
    if (!productDescription || [productDescription isEqualToString:@""]) {
        productDescription = @"商品";
    }
    /*
     *生成订单信息及签名
     */
    //将商品信息赋予AlixPayOrder的成员变量
    Order *order = [[Order alloc] init];
    order.partner = partner;
    order.seller = seller;
    
    if (huiDiaoZT == 1) {
        order.tradeNO = [NSString stringWithFormat:@"%@_%@_%@_%@",[self defaultUserId],type,tradeNO,count]; //订单ID（由商家自行制定）
        if ([type isEqualToString:@"100"]) {//电子卷支付传商户id
            NSArray *ary = [tradeNO componentsSeparatedByString:@"_"];
            order.tradeNO = [NSString stringWithFormat:@"%@_%@_%@_%@",[ary lastObject],type,[ary firstObject],count];
        }
        
        order.notifyURL =  ZFBkServerUrl; //回调URL
    }else if(huiDiaoZT == 2){
        order.tradeNO = [NSString stringWithFormat:@"%@_%@",tradeNO,type]; //订单ID（由商家自行制定）
//        order.notifyURL =  ZFBcheWeiUrl; //回调URL
    }else if(huiDiaoZT == 3){
        order.tradeNO = [NSString stringWithFormat:@"%@_%@",tradeNO,type]; //订单ID（由商家自行制定）
        order.notifyURL =  ZFBfangKeUrl; //回调URL
    }
    
    order.productName = productName; //商品标题
    order.productDescription = productDescription; //商品描述
    order.amount = [NSString stringWithFormat:@"%.2f",amount]; //商品价格
    //    order.amount = [NSString stringWithFormat:@"%.2f",0.02]; //商品价格
    
    order.service = @"mobile.securitypay.pay";
    order.paymentType = @"1";
    
    order.inputCharset = @"utf-8";
    order.itBPay = @"30m";
    order.showUrl = @"m.alipay.com";
    
    //应用注册scheme,在AlixPayDemo-Info.plist定义URL types
    NSString *appScheme = @"ping3232132123";
    
    //将商品信息拼接成字符串
    NSString *orderSpec = [order description];
    NSLog(@"orderSpec = %@",orderSpec);
    
    //获取私钥并将商户信息签名,外部商户可以根据情况存放私钥和签名,只需要遵循RSA签名规范,并将签名字符串base64编码和UrlEncode
    id<DataSigner> signer = CreateRSADataSigner(privateKey);
    NSString *signedString = [signer signString:orderSpec];
    
    //将签名成功字符串格式化为订单字符串,请严格按照该格式
    NSString *orderString = nil;
    if (signedString != nil) {
        orderString = [NSString stringWithFormat:@"%@&sign=\"%@\"&sign_type=\"%@\"",orderSpec, signedString,@"RSA"];
        //        @"partner="2088101568358171"&seller_id="xxx@alipay.com"&out_trade_no="0819145412-6177"&subject="测试"&body="测试测试"&total_fee="0.01"&notify_url="http://notify.msp.hk/notify.htm"&service="mobile.securitypay.pay"&payment_type="1"&_input_charset="utf-8"&it_b_pay="30m"&sign="lBBK%2F0w5LOajrMrji7DUgEqNjIhQbidR13GovA5r3TgIbNqv231yC1NksLdw%2Ba3JnfHXoXuet6XNNHtn7VE%2BeCoRO1O%2BR1KugLrQEZMtG5jmJIe2pbjm%2F3kb%2FuGkpG%2BwYQYI51%2BhA3YBbvZHVQBYveBqK%2Bh8mUyb7GM1HxWs9k4%3D"&sign_type="RSA""
        
        [[AlipaySDK defaultService] payOrder:orderString fromScheme:appScheme callback:^(NSDictionary *resultDic) {
            NSLog(@"reslut = %@",resultDic);
            
            block(resultDic);
        }];
    }
}


-(NSArray *)defaultChongZhiPhone
{
    NSUserDefaults *defaults=[NSUserDefaults standardUserDefaults];
    
    
    return [defaults objectForKey:@"ChongZhiPhone"];
    
}

-(void)defaultSaveChongZhiPhone:(NSArray*)a{
    NSUserDefaults *defaults=[NSUserDefaults standardUserDefaults];
    
    [defaults setObject:a  forKey:@"ChongZhiPhone"];
    
    [defaults synchronize];
}

-(NSString *)defaultUserId{
    NSUserDefaults *defaults=[NSUserDefaults standardUserDefaults];
    
    return [defaults objectForKey:@"UserId"];
}
-(void)defaultSaveUserId:(NSString*)a{
    NSUserDefaults *defaults=[NSUserDefaults standardUserDefaults];
    
    [defaults setObject:a  forKey:@"UserId"];
    
    [defaults synchronize];
}

-(NSString *)defaultLoadUserName{
    NSUserDefaults *defaults=[NSUserDefaults standardUserDefaults];
    return [defaults objectForKey:@"userName"];
}
-(void)defaultSaveUserName:(NSString*)a{
    NSUserDefaults *defaults=[NSUserDefaults standardUserDefaults];
    
    [defaults setObject:a  forKey:@"userName"];
    
    [defaults synchronize];
}

-(NSString *)defaultLoadUserPhone{
    NSUserDefaults *defaults=[NSUserDefaults standardUserDefaults];
    return [defaults objectForKey:@"mobilePhone"];
}
-(void)defaultSaveUserPhone:(NSString*)a{
    NSUserDefaults *defaults=[NSUserDefaults standardUserDefaults];
    
    [defaults setObject:a  forKey:@"mobilePhone"];
    [defaults synchronize];
}

-(NSString *)defaultPassWord{
    NSUserDefaults *defaults=[NSUserDefaults standardUserDefaults];
    
    
    return [defaults objectForKey:@"password"];
}

-(void)defaultSaveUserPassWord:(NSString*)a{
    NSUserDefaults *defaults=[NSUserDefaults standardUserDefaults];
    
    [defaults setObject:a  forKey:@"password"];
    [defaults synchronize];
}

-(BOOL)jianCeLogin{
    if ([self defaultLoadUserName] == nil || [[self defaultLoadUserName] isEqualToString:@""]) {
        DengLuViewController * deng = [[DengLuViewController alloc]init];
        if (_b == 1) {
            deng.a = 1;
        }
        [self.navigationController pushViewController:deng animated:YES];
    return NO;
    }
    return YES;
}
- (NSString*)dictionaryToJson:(NSDictionary *)dic
{
    NSError *parseError = nil;
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dic options:NSJSONWritingPrettyPrinted error:&parseError];
    NSString * a =[[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
    a = [a stringByReplacingOccurrencesOfString: @" " withString: @""];
    a = [a stringByReplacingOccurrencesOfString: @"\n" withString: @""];
    return a;
}
-(void)callWithTellNumber:(NSString *)tellNum{
    UIWebView *webView;
    if (webView == nil)
    {
        webView = [[UIWebView alloc] init];
    }
    [self.view addSubview:webView];
    NSURL *url = [NSURL URLWithString:[NSMutableString stringWithFormat:@"tel://%@",tellNum]];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [webView loadRequest:request];
}

-(NSString *)requestImageStr{
    NSArray *ary = @[@"http://img4q.duitang.com/uploads/item/201410/10/20141010170200_Tc3zT.jpeg",@"http://img4q.duitang.com/uploads/item/201408/11/20140811233422_dwtSU.jpeg",@"http://img.taopic.com/uploads/allimg/120724/201764-120H40P24256.jpg",@"http://cdn.duitang.com/uploads/item/201208/04/20120804013554_yRGfe.jpeg",@"http://img3.3lian.com/2013/c3/70/d/77.jpg"];
    
   int n =  arc4random()%(ary.count);
 
    return ary[n];
}

-(NSString *)returnNoNullStrWith:(NSString *)str andMoRenStr:(NSString *)moRenStr{
    
    if ([str isKindOfClass:[NSNull class]] || [str isEqual:[NSNull null]] || [str isEqual:@"(null)"] || str == nil) {
        if (moRenStr == nil) {
            return @"";
        }
        return moRenStr;
    }else{
        return str;
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
